// gulp 文件
"use strict";
var gulp = require("gulp");
var bom = require("gulp-bom");
var hash = require("gulp-static-hash");
var replace = require("gulp-replace");
var tar = require("gulp-tar");
var gzip = require("gulp-gzip");
var less = require("gulp-less");
var sass = require('gulp-ruby-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');
// 实时刷新
var browserSync = require('browser-sync');
var reload = browserSync.reload;

var fs = require("fs");
var conf = require("./config");

var env = process.argv[2];
var _conf;

// 删除文件
var del = require('del');


// 开发阶段

// 处理less文件
gulp.task('less', function() {
    gulp.src('view/less/*.*')
        .pipe(less())
        .pipe(gulp.dest('view/css'))
        .pipe(reload({ stream: true }));
});

// 监视文件改动并重新载入
gulp.task('dev', function() {
    browserSync({
        server: {
            baseDir: 'view'
        }
    });
    gulp.watch(['*.html', 'less/*.*', 'js/*.js', 'img/*.*'], { cwd: 'view' }, ['less', reload]);
});

// 生成阶段

// 删除build文件夹下的所有文件
gulp.task('remove', function() {
    del([
        'build/**/*'
    ])
})
gulp.task('copyJs', ['remove'], function() {
    gulp.src('view/js/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('build/js'))
})

gulp.task('copyImg', ['copyJs'], function() {
    gulp.src('view/img/*.*')
        .pipe(gulp.dest('build/img'))
})

gulp.task('buildCss', ['copyImg'], function() {
    gulp.src('view/css/*.css')
        .pipe(autoprefixer({
            browsers: ['> 5%', 'Android >= 4.0'],
            cascade: true, //是否美化属性值 默认：true 
            remove: true //是否去掉不必要的前缀 默认：true 
        }))
        .pipe(minifyCSS())
        .pipe(gulp.dest('build/css'));
})

gulp.task('build', ['buildCss'], function() {
    gulp.src('view/*.html')
        .pipe(hash({ asset: 'static' }))
        .pipe(gulp.dest('build'))
})